<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\CardBigService;
use app\model\card\CardBig as CardBigModel;
use think\facade\View;

class CardBig extends Base
{
    /**
     * 获取列表
     */
    public function index(CardBigService $cardBigService, \app\model\card\CardCate $cardCateModel)
    {
        if (request()->isAjax()) {

            $res = $cardBigService->getList(input('param.'))['data'];

            $cate = $cardCateModel->getAllList([
                'status' => 1
            ], 'id,name')['data']->toArray();
            
            $list['list'] = $res;
        

            return jsonReturn(0, 'success', $list);
        }
        return View::fetch();
    }

    /**
     * 添加
     */
    public function add(CardBigService $cardBigService)
    {
        if (request()->isPost()) {

            return json($cardBigService->addCardBig(input('post.'), 'add'));
        }

        if (request()->isAjax()) {

            return jsonReturn(0, 'success', $cardBigService->getBaseParam());
        }

        return View::fetch();
    }

    /**
     * 编辑
     */
    public function edit(CardBigService $cardBigService, \app\model\card\CardBig $cardBigModel)
    {
        if (request()->isPost()) {

            return json($cardBigService->editCardBig(input('post.'), 'edit'));
        }

        if (request()->isAjax()) {

            $id = input('param.id');
            $base = [
                'info' => $cardBigModel->findOne([
                    'id' => $id
                ])['data']
            ];

            $return = array_merge($base, $cardBigService->getBaseParam());

            return jsonReturn(0, 'success', $return);
        }

        return View::fetch();
    }

    /**
     * 上下架
     */
    public function shelf(CardBigModel $cardBigModel)
    {
        $param = input('post.');

        $res = $cardBigModel->updateByIds([
            'status' => $param['status']
        ], $param['ids']);

        $res['msg'] = '操作成功';
        return json($res);
    }

    /**
     * 删除商品
     */
    public function del(CardBigService $cardBigService)
    {
        return json($cardBigService->delCardBig(input('param.id')));
    }

    public function recover(CardBigModel $cardBigModel)
    {
        $res = $cardBigModel->updateByIds([
            'is_del' => 2
        ], input('param.id'));

        $res['msg'] = '操作成功';
        return json($res);
    }
}
